package org.boot.dao;

import org.boot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDao extends JpaRepository<User, Integer> {

    @Query(value = "select c.* from user c where c.id = %?1 FOR UPDATE", nativeQuery = true)
    User selectForUpdate(int id);

    // SELECT FOR UPDATE
    // 基于数据库锁机制来保证并发安全
    /**
     * -- 开启事务
     * START TRANSACTION;
     *
     * -- 查询并锁定数据
     * SELECT * FROM table_name WHERE condition FOR UPDATE;
     *
     * -- 执行数据更新或其他操作
     *
     * -- 提交事务，释放锁
     * COMMIT;
     */
}
